<?php

namespace catchAdmin\newsDeclare\controller;

use catchAdmin\newsDeclare\excel\newsDeclareExport;
use catchAdmin\customs\excel\orderExport;
use catchAdmin\newsDeclare\model\NewsDeclare;
use catchAdmin\newsDeclare\model\NewsReview;

use catchAdmin\permissions\model\Users;
use catcher\base\CatchRequest as Request;
use catcher\CatchAdmin;
use catcher\CatchAuth;
use catcher\CatchResponse;
use catcher\library\excel\Excel;
use think\facade\Db;

class Overview extends CatchAdmin
{
    public $field_string;

    public function __construct(NewsDeclare $newsDeclareModel,NewsReview $newsReviewModel,Users $newsUsersModel)
    {
        $this->newsDeclareModel = $newsDeclareModel;
        $this->newsReviewModel = $newsReviewModel;
        $this->newsUsersModel = $newsUsersModel;

        $sort_field_arr = [
            '人民日报社江苏分社',
            '新华社江苏分社',
            '中央广电总台江苏总站',
            '光明日报江苏记者站',
            '经济日报驻江苏记者站',
            '中国日报驻江苏记者站',
            '科技日报驻江苏记者站',
            '中国新闻社江苏分社',
            '工人日报驻江苏记者站',
            '中国青年报驻江苏记者站',
            '中国妇女报驻江苏记者站',
            '农民日报驻江苏记者站',
            '法治日报驻江苏记者站',
            '中国文化报江苏记者站',
            '中国工业报社江苏记者站',
            '上海证券报长三角新闻中心',
            '新华报业集团',
            '江苏广电总台（集团）',
            '江苏凤凰出版传媒集团（现代快报社）',
            '南京报业传媒集团',
            '南京广电集团',
            '人民网江苏频道',
            '新华网江苏频道',
            '央视网江苏频道',
            '澎湃新闻',
            '江宁区融媒体中心',
            '浦口区融媒体中心',
            '六合区融媒体中心',
            '溧水区融媒体中心',
            '高淳区融媒体中心',
            '江北新区融媒体中心',
            '玄武区融媒体中心',
            '秦淮区融媒体中心',
            '建邺区融媒体中心',
            '鼓楼区融媒体中心',
            '栖霞区融媒体中心',
            '雨花台区融媒体中心',
        ];

        // 使用 array_map 函数和匿名函数添加单引号
        $sort_field_arr = array_map(function($item) {
            return "'" . $item . "'";
        }, $sort_field_arr);

        $string = implode(", ", $sort_field_arr);
        $this->field_string  = "FIELD(u.rec_unit, $string)";
    }

    //申报
    public function sb(CatchAuth $auth,Request $request){
        $where = [];
        $where[] = ['u.tenant_id','=',10052];
        $where[] = ['u.position','=',101];
        $where[] = ['u.rec_unit','not in',['央广网江苏频道','交汇点新闻','荔枝网、荔枝新闻','紫牛新闻','龙虎网','南京发布','ceshi0001']];
//        $where[] = ['nd.is_declare','=',1];

        $result = $this->newsUsersModel
            ->alias('u')
            ->field('u.rec_unit,count(nd.id) as article_count,u.declare_info,u.rec_num,u.id,u.reporter,u.reporter_phone,u.reporter_job')
            ->leftJoin('db_news_declare nd','nd.creator_id = u.id and nd.is_declare = 1')
            ->where($where)
            ->orderRaw($this->field_string)
            ->group('u.id')
            ->select()
        ;

        return CatchResponse::success($result);
    }

    //评委评审 情况
    public function pwps(CatchAuth $auth,Request $request){
        $where = [];
        $where[] = ['u.tenant_id','=',10052];
        $where[] = ['u.position','=',100];
//        $where[] = ['nd.is_declare','=',1];

        $result = $this->newsUsersModel
            ->alias('u')
            ->field('u.username,u.rec_unit,count(nr.id) as judge_article_count,u.declare_info')
//            ->leftJoin('db_news_declare nd','nd.creator_id = u.id and nd.is_declare = 1')
            ->leftJoin('db_news_review nr','nr.judge_id = u.id')
            ->where($where)
//            ->orderRaw($this->field_string)
            ->group('u.id')
            ->select()
        ;

        return CatchResponse::success($result);
    }

    //评委评审 结果
    public function psjg(CatchAuth $auth,Request $request){

    }

    //订单导出
    public function export(Request $request, Excel $excel, newsDeclareExport $newsDeclareExport){
        //return CatchResponse::success($newsDeclareExport->sheets());

        $data = $excel->save($newsDeclareExport, 'export/newsDeclare');
        $data['url'] = str_replace('127.0.0.1:8000','cross.qekj.net',$data['url']);
        return CatchResponse::success($data);
    }

    //取消申报
    public function cancelSb($id, CatchAuth $auth,Request $request){
        //$user = $auth->user();
        $user = Users::where('id',$id)->find();
        $type = $request->put('type','xw');
        $type_1 = $request->put('type_1');

        $declare_info = $user['declare_info'];
        if(empty($declare_info)){
            $declare_info = [];
        }
        if($type  == 'xw'){
            $unset_key = 'xw_sb';
            $_type_name ='新闻奖';
        }elseif(substr($type,0,2)  === 'cb'){
            $unset_key = $type . "_sb";
            $_type_name ='传播奖';
        }

        if(array_key_exists($unset_key,$declare_info)){
            unset($declare_info[$unset_key]);
        }
        $user->declare_info = $declare_info;

        Db::startTrans();
        try {
            $user->save();

            $where = ['type'=>$_type_name,'creator_id'=>$id];
            if($type_1){
                $where['type_1'] = $type_1;
            }

            $this->newsDeclareModel::update([
                'is_declare'=>0
            ],$where);

            Db::commit();
        }catch (\Exception $exception) {
            Db::rollback();
            return CatchResponse::fail('取消失败');
        }

        return CatchResponse::success();
    }
}